package net.kindroid.fm;

import java.util.ArrayList;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextPaint;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ChannelList extends Activity
{
	/**
	 * View inflater
	 */
	private LayoutInflater mInflater = null;

	/**
	 * cursor
	 */
	private Cursor cursor = null;

	/**
	 * channel list
	 */
	private ListView channelList = null;

	/**
	 * delete
	 */
	private Button bt_delete = null;

	/**
	 * cancel
	 */
	private Button bt_cancel = null;

	/**
	 * Button Layout
	 */
	private LinearLayout bt_layout = null;

	/**
	 * list empty view
	 */
	private TextView empty_tx = null;

	/**
	 * delete mode
	 */
	private boolean isDeleteMode = false;

	/**
	 * channel list adapter
	 */
	private ListCursorAdapter adapter = null;

	/**
	 * select cursor
	 */
	private ArrayList<Integer> selected = null;

	/**
	 * screen width
	 */
	public int screenWidth = 0;

	/**
	 * screen height
	 */
	public int screenHeight = 0;

	/**
	 * current play channel
	 */
	private int currentChannel = 0;

	/**
	 * is current play
	 */
	private boolean isPlaying = false;

	/**
	 * delete dialog
	 */
	private MyDialog deleteConfirmDialog = null;

	/**
	 * long click dialog
	 */
	private MyDialog longClickDialog = null;

	/**
	 * edit channel dialog
	 */
	private MyDialog editChannelDialog = null;

	/**
	 * select all
	 */
	private static final int MENU_SELECT_ALL = 0;

	/**
	 * select none
	 */
	private static final int MENU_DESELECT_ALL = MENU_SELECT_ALL + 1;

	/**
	 * delete mode
	 */
	private static final int MENU_DELETE = MENU_DESELECT_ALL + 1;

	/**
	 * auto search channel
	 */
	private static final int MENU_SEARCH = MENU_DELETE + 1;

	/**
	 * delete done
	 */
	private static final int DELETE_DONE = 0;
	
	/**
	 * show toast
	 */
	private Toast toast = null;

	/**
	 * handler
	 */
	private Handler mHandle = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			switch (msg.what)
			{
				case DELETE_DONE:
				{
					getData();
					isDeleteMode = false;
					if (bt_layout != null)
					{
						bt_layout.setVisibility(View.GONE);
					}
					adapter.notifyDataSetChanged();
					break;
				}
			}
		}
	};

	@Override
	public boolean onKeyUp(int arg0, KeyEvent arg1)
	{
		if (arg0 == KeyEvent.KEYCODE_BACK)
		{
			if (isDeleteMode)
			{
				isDeleteMode = false;
				if (bt_layout != null)
				{
					bt_layout.setVisibility(View.GONE);
				}
				adapter.notifyDataSetChanged();
				return true;
			} else
			{
				finish();
			}

		}
		return super.onKeyUp(arg0, arg1);
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.channellistlayout);

		getViews();
		addEvents();
		initData();
		getData();
		setListViewAdapter();
	}

	private void initData()
	{
		currentChannel = getIntent().getIntExtra("currentChannel", 0);
		isPlaying = getIntent().getBooleanExtra("isPlaying", false);
		empty_tx.setTextSize(16);
	}
	/**
	 * 获取View引用
	 */
	private void getViews()
	{
		channelList = (ListView) this.findViewById(R.id.channelList);
		bt_layout = (LinearLayout) this.findViewById(R.id.list_bt_layout);
		bt_delete = (Button) this.findViewById(R.id.list_bt_delete);
		bt_cancel = (Button) this.findViewById(R.id.list_bt_cancel);
		empty_tx = (TextView) this.findViewById(R.id.list_empty_text);
		if (bt_layout != null)
		{
			bt_layout.setVisibility(View.GONE);
		}
		if (empty_tx != null)
		{
			empty_tx.setVisibility(View.GONE);
		}

		mInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		final Display display = this.getWindow().getWindowManager().getDefaultDisplay();
		if (display != null)
		{
			// screen width
			screenWidth = display.getWidth();
			screenHeight = display.getHeight();
		}
	}

	/**
	 * add events
	 */
	private void addEvents()
	{
		channelList.setFadingEdgeLength(0);
		channelList.setCacheColorHint(0);
		channelList.setOnItemLongClickListener(itemLongClickListener);
		channelList.setOnItemClickListener(itemClickListener);

		bt_delete.setOnClickListener(btClickListener);
		bt_cancel.setOnClickListener(btClickListener);

		selected = new ArrayList<Integer>();
		registerReceiver(headSetReceiver, new IntentFilter(FMService.CLOSE_FM));
	}

	/**
	 * headset listener
	 */
	private final BroadcastReceiver headSetReceiver = new BroadcastReceiver()
	{
		public void onReceive(Context context, Intent intent)
		{
			String action = intent.getAction();
			if (action.equals(FMService.CLOSE_FM))
			{
				finish();
			}
		}
	};

	@Override
	protected void onPause()
	{
		FMService.isActivityFocus = false;
		super.onPause();
	}

	@Override
	protected void onResume()
	{
		FMService.isActivityFocus = true;
		super.onResume();
	}

	@Override
	protected void onDestroy()
	{
		if (cursor != null)
		{
			cursor.close();
			cursor = null;
		}
		unregisterReceiver(headSetReceiver);
		super.onDestroy();
	}

	/**
	 * get data from database
	 */
	private void getData()
	{
		ContentResolver resolver = getContentResolver();
		if (cursor != null)
		{
			cursor.close();
		}
		cursor = null;

		// channel list
		String order = FMInfo.FMTable.TUNE + " ASC";
		cursor = resolver.query(FMInfo.FMTable.CONTENT_URI, null, null, null, order);

		checkListIsEmpty();
	}

	/**
	 * show channel list
	 */
	private void setListViewAdapter()
	{
		adapter = new ListCursorAdapter();
		channelList.setAdapter(adapter);
	}

	/**
	 * whether list is empty
	 */
	private void checkListIsEmpty()
	{
		if (cursor == null || cursor.getCount() <= 0)
		{
			if (empty_tx != null)
			{
				empty_tx.setVisibility(View.VISIBLE);
			}

			if (channelList != null)
			{
				channelList.setVisibility(View.GONE);
			}
		} else
		{
			if (empty_tx != null)
			{
				empty_tx.setVisibility(View.GONE);
			}

			if (channelList != null)
			{
				channelList.setVisibility(View.VISIBLE);
			}
		}
	}

	/**
	 * List Item long click listener
	 */
	private OnItemLongClickListener itemLongClickListener = new OnItemLongClickListener()
	{
		@Override
		public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
		{
			if (!isDeleteMode)
			{
				try
				{
					ViewHolder hold = (ViewHolder) view.getTag();
					showLongClickDialog(hold);
				} catch (NumberFormatException e)
				{
				}
			}
			return false;
		}
	};

	/**
	 * List Item click listener
	 */
	private OnItemClickListener itemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
		{
			switch (arg0.getId())
			{
				case R.id.channelList:
				{
					if (isDeleteMode)
					{
						ViewHolder hold = (ViewHolder) arg1.getTag();
						int id = hold._id;
						if (selected.contains(new Integer(id)))
						{
							selected.remove(new Integer(id));
							hold.checkBox.setChecked(false);
						} else
						{
							selected.add(new Integer(id));
							hold.checkBox.setChecked(true);
						}
						adapter.notifyDataSetChanged();
						showSelectNum();
					} else
					{
						try
						{
							// set play the channel
							ViewHolder hold = (ViewHolder) arg1.getTag();
							int tune = Integer.parseInt(hold.tune.getTag().toString());
							SharedPreferences preferences = getSharedPreferences("fmradio", 0);
							SharedPreferences.Editor editor = preferences.edit();
							editor.putInt("list_tune", tune);
							editor.commit();
							Intent intent = new Intent();
							intent.putExtra("list_tune", tune);
							setResult(Activity.RESULT_OK, intent);
							finish();
						} catch (NumberFormatException e)
						{
						}
					}
					break;
				}
				case R.id.long_click_content_list:
				{
					if (arg2 == 0)
					{
						// edit
						showEditChannelDialog();
					} else if (arg2 == 1)
					{
						// delete
						showDeleteConfirmDialog();
					}

					if (longClickDialog != null)
					{
						longClickDialog.dismiss();
						longClickDialog = null;
					}
					break;
				}
			}
		}
	};

	/**
	 * button click listener
	 */
	private OnClickListener btClickListener = new OnClickListener()
	{
		@Override
		public void onClick(View arg0)
		{
			switch (arg0.getId())
			{
				case R.id.list_bt_delete:
				{
					if (selected.size() > 0)
					{
						showDeleteConfirmDialog();
					} else
					{
						showToast(R.string.delete_empty_info);
					}
					break;
				}
				case R.id.list_bt_cancel:
				{
					isDeleteMode = false;
					if (bt_layout != null)
					{
						bt_layout.setVisibility(View.GONE);
					}
					adapter.notifyDataSetChanged();
					break;
				}
				case R.id.delete_button_Ok:
				{
					if (deleteConfirmDialog != null)
					{
						deleteConfirmDialog.dismiss();
						deleteConfirmDialog = null;
					}

					deleteSelected();
					break;
				}
				case R.id.delete_button_Cancel:
				{
					if (deleteConfirmDialog != null)
					{
						deleteConfirmDialog.dismiss();
						deleteConfirmDialog = null;
					}
					break;
				}
			}
		}
	};

	/**
	 * delete selected
	 */
	private void deleteSelected()
	{
		new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				if (selected.size() > 0)
				{
					ContentResolver resolver = getContentResolver();
					for (int id : selected)
					{
						String where = FMInfo.FMTable._ID + " = " + id;
						resolver.delete(FMInfo.FMTable.CONTENT_URI, where, null);
						where = null;
					}
				}

				mHandle.sendEmptyMessage(DELETE_DONE);
			}
		}).start();
	}

	/**
	 * sure delete dialog
	 */
	private void showDeleteConfirmDialog()
	{
		deleteConfirmDialog = new MyDialog(this);
		LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.delete_comform_layout, null);
		int size = 0;
		if (screenWidth > screenHeight)
		{
			size = screenWidth;
		} else
		{
			size = screenHeight;
		}

		// set dialog size
		deleteConfirmDialog.setDialogSize((int) (size * 0.55), (int) (size * 0.27));
		deleteConfirmDialog.setMyContentView(layout);
		deleteConfirmDialog.show();

		ImageButton title_icon = (ImageButton) layout.findViewById(R.id.delete_title_icon);
		TextView title_name = (TextView) layout.findViewById(R.id.delete_title_text);

		TextView content = (TextView) layout.findViewById(R.id.delete_content_text);

		LinearLayout bottom_layout = (LinearLayout) layout.findViewById(R.id.delete_bottom_layout);
		bottom_layout.setBackgroundColor(Color.WHITE);
		Button ok = (Button) layout.findViewById(R.id.delete_button_Ok);
		Button cancel = (Button) layout.findViewById(R.id.delete_button_Cancel);

		title_icon.setBackgroundResource(R.drawable.delete_confirm);

		int textSize = 18;
		title_name.setTextSize(textSize);
		title_name.setTextColor(Color.WHITE);
		title_name.setText(R.string.delete_confirm_title);

		content.setTextSize(textSize);
		content.setTextColor(Color.WHITE);
		content.setText(R.string.delete_confirm_content);

		ok.setText(R.string.ok);
		ok.setTextColor(Color.BLACK);
		ok.setTextSize(textSize);
		ok.setOnClickListener(btClickListener);

		cancel.setText(R.string.cancel);
		cancel.setTextColor(Color.BLACK);
		cancel.setTextSize(textSize);
		cancel.setOnClickListener(btClickListener);
	}

	/**
	 * select number
	 */
	private void showSelectNum()
	{
		if (selected == null)
		{
			selected = new ArrayList<Integer>();
		}

		bt_delete.setText(this.getResources().getString(R.string.bt_delete, selected.size()));
	}

	/**
	 * select all
	 */
	private void selectAll()
	{
		if (selected == null)
		{
			selected = new ArrayList<Integer>();
		}
		selected.clear();

		if (cursor != null && cursor.moveToFirst())
		{
			while (!cursor.isAfterLast())
			{
				selected.add(new Integer(cursor.getInt(0)));
				cursor.moveToNext();
			}
		}
		showSelectNum();
		adapter.notifyDataSetChanged();
	}

	/**
	 * select none
	 */
	private void selectNone()
	{
		if (selected == null)
		{
			selected = new ArrayList<Integer>();
		}
		selected.clear();
		showSelectNum();
		adapter.notifyDataSetChanged();
	}

	/**
	 * long click dialog
	 */
	private void showLongClickDialog(ViewHolder hold)
	{
		longClickDialog = new MyDialog(this);
		LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.long_click_layout, null);
		int size = 0;
		if (screenWidth > screenHeight)
		{
			size = screenWidth;
		} else
		{
			size = screenHeight;
		}

		// set dialog size
		longClickDialog.setDialogSize((int) (size * 0.45), (int) (size * 0.5));
		longClickDialog.setMyContentView(layout);
		longClickDialog.show();

		ImageButton title_icon = (ImageButton) layout.findViewById(R.id.longclick_title_icon);
		TextView title_name = (TextView) layout.findViewById(R.id.longclick_title_text);
		ListView list = (ListView) layout.findViewById(R.id.long_click_content_list);

		title_icon.setBackgroundResource(R.drawable.delete_confirm);

		title_name.setTextSize(18);
		title_name.setTextColor(Color.WHITE);
		title_name.setText(hold.tune.getText());

		ArrayList<String> data = new ArrayList<String>();
		data.add(this.getText(R.string.edit_channel).toString());
		data.add(this.getText(R.string.delete).toString());

		LongClickAdapter adapter = new LongClickAdapter(data);
		list.setAdapter(adapter);
		list.setBackgroundResource(R.color.white);
		list.setFadingEdgeLength(0);
		list.setOnItemClickListener(itemClickListener);
		list.setCacheColorHint(0);
		list.setFocusable(true);

		adapter.notifyDataSetChanged();

		if (selected == null)
		{
			selected = new ArrayList<Integer>();
		}
		selected.clear();
		selected.add(new Integer(hold._id));
	}
	
	private void showToast(int resId)
	{
		if(toast != null)
		{
			toast.setText(resId);
			toast.show();
		} else
		{
			toast = Toast.makeText(ChannelList.this, resId, Toast.LENGTH_SHORT);
			toast.show();
		}
	}
	
	/**
	 * edit channel dialog
	 */
	private void showEditChannelDialog()
	{
		editChannelDialog = new MyDialog(this);
		LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.edit_channel_layout, null);
		int size = 0;
		if (screenWidth > screenHeight)
		{
			size = screenWidth;
		} else
		{
			size = screenHeight;
		}

		// set dialog size
		editChannelDialog.setDialogSize((int) (size * 0.55), (int) (size * 0.37));
		editChannelDialog.setMyContentView(layout);
		editChannelDialog.show();

		ImageButton title_icon = (ImageButton) layout.findViewById(R.id.edit_title_icon);
		TextView title_name = (TextView) layout.findViewById(R.id.edit_title_text);

		final TextView tune_name = (TextView) layout.findViewById(R.id.edit_content_tune_title);
		TextView tune_value = (TextView) layout.findViewById(R.id.edit_content_tune_name);

		TextView tune_title = (TextView) layout.findViewById(R.id.edit_content_name_title);
		final EditText input_title = (EditText) layout.findViewById(R.id.edit_content_name_input);

		Button ok = (Button) layout.findViewById(R.id.edit_button_Ok);
		Button cancel = (Button) layout.findViewById(R.id.edit_button_Cancel);

		final int id = selected.get(0);

		ContentResolver resolver = getContentResolver();
		String where = FMInfo.FMTable._ID + " = " + id;
		Cursor cur = resolver.query(FMInfo.FMTable.CONTENT_URI, null, where, null, null);
		int channel_Value = 0;
		String channel_Name = "";
		if (cur != null && cur.moveToFirst())
		{
			channel_Value = cur.getInt(cur.getColumnIndex(FMInfo.FMTable.TUNE));
			channel_Name = cur.getString(cur.getColumnIndex(FMInfo.FMTable.TITLE));
		}

		int textSize = 18;
		title_icon.setBackgroundResource(R.drawable.delete_confirm);
		title_name.setTextSize(textSize);
		title_name.setTextColor(Color.WHITE);
		title_name.setText(this.getText(R.string.edit_channel));

		float tune = channel_Value / 10.0f;
		tune_name.setTextSize(textSize);
		tune_name.setTextColor(Color.WHITE);
		tune_name.setText(this.getText(R.string.channel));
		tune_value.setTextSize(textSize);
		tune_value.setTextColor(Color.WHITE);
		tune_value.setText(tune + " MHZ");

		tune_title.setTextSize(textSize);
		tune_title.setTextColor(Color.WHITE);
		tune_title.setText(this.getText(R.string.title));
		input_title.setTextSize(textSize);
		input_title.setTextColor(Color.BLACK);
		input_title.setText(channel_Name);
		input_title.setSelection(input_title.getText().length());

		ok.setText(R.string.ok);
		cancel.setText(R.string.cancel);

		ok.setTextColor(Color.BLACK);
		cancel.setTextColor(Color.BLACK);

		ok.setTextSize(textSize);
		cancel.setTextSize(textSize);

		ok.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				String text = input_title.getText().toString();
				if(text == "" || text.equals(""))
				{
					showToast(R.string.title_hint);
					return;
				}
				ContentResolver resolver = getContentResolver();
				String where = FMInfo.FMTable._ID + " = " + id;
				ContentValues values = new ContentValues();
				values.put(FMInfo.FMTable.TITLE, text);
				values.put(FMInfo.FMTable.COMMENT, 1);

				int status = resolver.update(FMInfo.FMTable.CONTENT_URI, values, where, null);

				if (editChannelDialog != null)
				{
					editChannelDialog.dismiss();
					editChannelDialog = null;
				}

				getData();
				adapter.notifyDataSetChanged();
				if (status > 0)
				{
					showToast(R.string.edit_success);
				} else
				{
					showToast(R.string.edit_error);
				}
			}
		});

		cancel.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				if (editChannelDialog != null)
				{
					editChannelDialog.dismiss();
					editChannelDialog = null;
				}
			}
		});
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item)
	{
		switch (item.getItemId())
		{
			case MENU_DELETE:
			{
				isDeleteMode = true;
				if (bt_layout != null)
				{
					bt_layout.setVisibility(View.VISIBLE);
				}
				selected.clear();

				showSelectNum();
				adapter.notifyDataSetChanged();
				break;
			}
			case MENU_SELECT_ALL:
			{
				selectAll();
				break;
			}
			case MENU_DESELECT_ALL:
			{
				selectNone();
				break;
			}
			case MENU_SEARCH:
			{
				Intent intent = new Intent();
				intent.putExtra("list_tune", -1);
				setResult(Activity.RESULT_OK, intent);

				finish();
				break;
			}
		}
		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu)
	{
		menu.removeItem(MENU_SELECT_ALL);
		menu.removeItem(MENU_DESELECT_ALL);
		menu.removeItem(MENU_DELETE);
		menu.removeItem(MENU_SEARCH);

		if (cursor == null || cursor.getCount() <= 0)
		{
			menu.add(0, MENU_SEARCH, 0, this.getResources().getString(R.string.menu_search));
		} else
		{
			if (isDeleteMode)
			{
				menu.add(0, MENU_SELECT_ALL, 1, this.getResources().getString(R.string.select_all));
				menu.add(0, MENU_DESELECT_ALL, 2, this.getResources().getString(R.string.select_none));
			} else
			{
				menu.add(0, MENU_DELETE, 2, this.getResources().getString(R.string.delete)).setIcon(getMenuIconResource(MENU_DELETE));
			}
		}
		return super.onPrepareOptionsMenu(menu);
	}

	private int getMenuIconResource(int res)
	{
		int result = 0;
		switch (res)
		{
			case MENU_DELETE:
			{
				result = R.drawable.delete;
				break;
			}
			case MENU_SELECT_ALL:
			{
				result = R.drawable.headset;
				break;
			}
			case MENU_DESELECT_ALL:
			{
				result = R.drawable.external;
				break;
			}
			case MENU_SEARCH:
			{
				result = R.drawable.search;
				break;
			}
		}
		return result;
	}

	private class ListCursorAdapter extends BaseAdapter
	{
		@Override
		public int getCount()
		{
			if (cursor != null)
			{
				return cursor.getCount();
			}
			return 0;
		}

		@Override
		public Object getItem(int arg0)
		{
			if (cursor != null && cursor.getCount() > 0)
			{
				return cursor.moveToPosition(arg0) ? cursor : null;
			}
			return null;
		}

		@Override
		public long getItemId(int arg0)
		{
			if (cursor != null && cursor.getCount() > 0)
			{
				return cursor.moveToPosition(arg0) ? cursor.getLong(0) : null;
			}
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2)
		{
			ViewHolder hold = null;
			if (arg1 == null)
			{
				hold = new ViewHolder();
				arg1 = mInflater.inflate(R.layout.channellist_item, null);
				hold.tune = (TextView) arg1.findViewById(R.id.tune);
				TextPaint tp = hold.tune.getPaint();
				tp.setFakeBoldText(true);

				hold.title = (TextView) arg1.findViewById(R.id.title);
				hold.checkBox = (CheckBox) arg1.findViewById(R.id.checkbox);
				hold.image = (ImageView) arg1.findViewById(R.id.image);
				arg1.setTag(hold);
			} else
			{
				hold = (ViewHolder) arg1.getTag();
			}

			if (arg0 < cursor.getCount())
			{
				cursor.moveToPosition(arg0);
				int tune = cursor.getInt(cursor.getColumnIndex("tune"));
				hold.tune.setText((tune / 10f) + " MHZ");
				hold.tune.setTag("" + tune);
				if(tune == currentChannel && isPlaying)
				{
					hold.image.setImageDrawable(getResources().getDrawable(R.drawable.fm_play_status_list));
				} else
				{
					hold.image.setImageDrawable(null);
				}
				String title = cursor.getString(cursor.getColumnIndex("title"));
				if (title != null && title.length() > 0)
				{
					hold.title.setText(title);
				} else
				{
					title = ChannelList.this.getText(R.string.channel_no_name).toString();
					hold.title.setText(title);
				}

				hold._id = cursor.getInt(0);
			}

			if (hold.checkBox != null && isDeleteMode)
			{
				hold.checkBox.setButtonDrawable(R.drawable.checkbox_background);
				hold.checkBox.setFocusable(false);
				hold.checkBox.setVisibility(View.VISIBLE);
				hold.checkBox.setOnClickListener(checkboxClickListener);
				hold.checkBox.setTag("" + hold._id);

				if (selected.contains(new Integer(hold._id)))
				{
					hold.checkBox.setChecked(true);
				} else
				{
					hold.checkBox.setChecked(false);
				}
			} else
			{
				if (selected == null)
				{
					selected = new ArrayList<Integer>();
				}
				selected.clear();

				hold.checkBox.setVisibility(View.GONE);
			}
			return arg1;
		}

		/**
		 * delete check box click listener
		 */
		private OnClickListener checkboxClickListener = new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				if (arg0 instanceof CheckBox)
				{
					CheckBox checkbox = (CheckBox) arg0;
					int id = Integer.parseInt(checkbox.getTag().toString());
					if (checkbox.isChecked())
					{
						if (!selected.contains(new Integer(id)))
						{
							selected.add(new Integer(id));
						}
					} else
					{
						selected.remove(new Integer(id));
					}

					showSelectNum();
				}
			}
		};
	}

	public final class ViewHolder
	{
		public int _id = -1;
		public TextView tune = null;
		public TextView title = null;
		public ImageView image = null;
		public CheckBox checkBox = null;
	}

	public class LongClickAdapter extends BaseAdapter
	{
		/**
		 * resourcData
		 */
		ArrayList<String> resourcData = null;

		public void setData(ArrayList<String> data)
		{
			if (resourcData != null)
			{
				resourcData.clear();
			}
			resourcData = null;
			resourcData = data;
		}

		public LongClickAdapter(ArrayList<String> data)
		{
			super();
			setData(data);
		}

		@Override
		public int getCount()
		{
			if (resourcData == null)
			{
				return 0;
			}
			return resourcData.size();
		}

		@Override
		public Object getItem(int arg0)
		{
			if (resourcData == null)
			{
				return null;
			}
			return resourcData.get(arg0);
		}

		@Override
		public long getItemId(int arg0)
		{
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2)
		{
			LinearLayout layout = null;
			TextView name = null;
			if (arg1 == null)
			{
				layout = (LinearLayout) LayoutInflater.from(ChannelList.this).inflate(R.layout.long_click_item, null);
			} else
			{
				layout = (LinearLayout) arg1;
			}

			name = (TextView) layout.findViewById(R.id.long_click_item_name);
			name.setText(resourcData.get(arg0));
			name.setTextColor(Color.BLACK);
			name.setTextSize(20);

			layout.setFocusable(false);
			return layout;
		}
	}
}
